﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace NCSCS
{
    public partial class MYiBiao : UserControl
    {
        float m_value=0;
        float m_maxV = 0;
        int ngrid = 10;
        Bitmap bm;
        string m_dw;
        public string Dw
        {
            get 
            { 
                return m_dw; 
            }
            set
            {
                m_dw = value;
            }
        }
        public int Ngrid
        {
            get
            { 
                return ngrid;
            }
            set
            {
                ngrid  = value;
            }
        }
        public MYiBiao()
        {
            InitializeComponent();
            bm = new Bitmap(this.Width, this.Height);
            //float v = 55;// VBMath.Rnd(1.5f); 
            //MaxV = v;
            //Value = v;
        }
        public float Value
        {
            get { return m_value; }
            set {
                m_value = value;
                if (m_value > m_maxV)
                { m_value = m_maxV; }
                redraw();
                this.Refresh();
            }
        }
        public float MaxV
        {
            get { return m_maxV; }
            set {
                m_maxV = value;// getNearest(value);
                if (m_value > m_maxV)
                { m_value = m_maxV; }
                redraw();
                this.Refresh();
            }
        }
        float getNearest(float v)
        {
        //    return v;
        //}
        //int getGridNumber(float v)
        //{
            string s = string.Format("{0:E}", v);
            string[] s1=s.Split('E');
            float x,y;
            float.TryParse(s1[0],out x);
            float.TryParse(s1[1], out y);
            int n0 = (int)x;
            int n = n0;
            float zheng;
            if((float)n<x)
            {
                zheng = n0 * (float)Math.Pow((double)10, (double)y);
                while (zheng < v)
                {
                    zheng = zheng + zheng / n;
                    n = n + 1;
                }
            }
            else
            {
                zheng = x * (float)Math.Pow((double)10, (double)y);
            }
            switch (n)
            {
                case 1:
                    n = 10;
                    break;
                case 2:
                    n = 10;
                    break;
                case 3:
                    n = 10; break;
                case 4:
                    n = 10; break;
                case 5:
                    n = 10; break;
            }
            ngrid = n;
            return zheng;
        }

        private void redraw()
        {
            Graphics graphics = Graphics.FromImage(bm); //this.CreateGraphics();
            //设置路码表的高和宽
            float WIDTH = this.Width;
            float HEIGHT = this.Height;// clientrect.Height;
            float bili =HEIGHT/ 300f  ;
            //设置文本输出外观
            graphics.SmoothingMode = SmoothingMode.AntiAlias;

            FontFamily fontFamily = new FontFamily("Times New Roman");
            Font font = new Font(fontFamily, 14, FontStyle.Bold, GraphicsUnit.Pixel);

            //将绘图平面的坐标原点移到窗口中心
            graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
            //绘制仪表盘
            //Brush b=graphics.c
            graphics.FillEllipse(new SolidBrush(System.Drawing.SystemColors.GradientInactiveCaption),
                HEIGHT / -2, HEIGHT / -2, HEIGHT, HEIGHT);
            //输出文本
            //graphics.DrawString("公里/小时", font, Brushes.Bisque
            //    , new PointF(-26, HEIGHT/2-HEIGHT / 5 - font.Height));
            graphics.DrawString(string.Format("{0:F1}", m_value) + m_dw, font, Brushes.Bisque
                , new PointF(-36, HEIGHT / 2 - HEIGHT / 5 - font.Height));
            //绘制刻度标记
            graphics.RotateTransform(225);
            //int ngrid = getGridNumber(maxV);
            int n1 = ngrid * 5;
            for (int x = 0; x < n1 + 1; x++)
            {
                graphics.FillRectangle(Brushes.Azure, -2*bili, (HEIGHT / 2 - 2*bili) * -1, 3*bili, 15*bili);
                graphics.RotateTransform(270f / n1);
            }

            //重置绘图平面的坐标变换
            graphics.ResetTransform();

            graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
            graphics.RotateTransform(225);
            float sp = 0;
            string tmp = string.Empty;
            //绘制刻度值(整数类)
            int n = ngrid;
            for (int x = 0; x < n + 1; x++)
            {
                tmp = string.Format("{0:F0}", sp);

                //绘制红色刻度
                graphics.FillRectangle(Brushes.Bisque, -3*bili, (HEIGHT / 2 - 2*bili) * -1, 6*bili, 25*bili);
                //绘制数值
                graphics.DrawString(tmp, font, Brushes.Bisque, new PointF(tmp.Length * (-5),
                    (HEIGHT / -2) + 25*bili));
                //旋转45度
                graphics.RotateTransform(270.0f / n);
                sp += m_maxV / n;
            }

            //重置绘图平面的坐标变换
            graphics.ResetTransform();
            graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);

            //绘制指针在30公里/秒的情形
            float angle = (m_value / m_maxV) * 270f + 225;
            graphics.RotateTransform((float)angle);

            Pen pen = new Pen(Color.Chocolate, 8*bili);
            //设置线帽
            pen.EndCap = LineCap.ArrowAnchor;
            pen.StartCap = LineCap.RoundAnchor;
            //画指针
            graphics.DrawLine(pen, new PointF(0, 0), new PointF(0, (-1) * (HEIGHT / 2.75F)));

            graphics.ResetTransform();
            graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
            //绘制中心点
            //graphics.FillEllipse(Brushes.Black, -6, -9, 14, 14);
            graphics.FillEllipse(Brushes.ForestGreen, -7*bili, -7*bili, 14*bili, 14*bili);
            //

        }
        private void MYiBiao_Paint(object sender, PaintEventArgs e)
        {
            //redraw();
            Graphics g = this.CreateGraphics();
            g.DrawImage(bm, 0, 0); 
        }

        private void MYiBiao_Resize(object sender, EventArgs e)
        {
            bm = new Bitmap(this.Width, this.Height);
            redraw();
        }
    }
}
